package plgbase

/**

设计思路:
  1. 初步思路 2020-11-20 09:00:15

数据入口:
  订阅
  写入

数据出口:
  订阅中心分发:
  listener分发:


数据源->加密->输出


数据生产者:
  数据源
  RSA加密

数据消费者:
  RAS加密
  数据输出



数据生产者:
  可以设置一名消费者, 直接调用消费者函数进行消费
  可以设置一个订阅中心, 进行数据分发

数据消费者:
  实现一个函数, 提供数据输入
  设置一个订阅中心, 进行消息订阅

*/
import (
	"dgo/goutils/utils"
)

const (
	// plugid, plug
	PLUG_EVENT_BEFORE_CLOSE string = "plug/beforeRelease"

	//
	PLUG_DISP_RELEASE string = "plug/release"

	// 通知插件重载
	// plug, plugid
	PLUG_DISP_CHANGED string = "plug/changed"
)

/*
*

	将外面的ID设置进去,让插件知道自己的ID
*/
type IPlugin_SetID interface {
	SetPlugID(val string)
}

type IPlugin_GetPlugID interface {
	GetPlugID() string
}

type IPlugin_IsRelease interface {
	IsRelease() bool
}

func PluginIsRelease(v interface{}) int {
	if intf, ok := v.(IPlugin_IsRelease); ok {
		if intf.IsRelease() {
			return 1
		} else {
			return 0
		}
	} else {
		return -1
	}
}

func GetPlugID(obj interface{}, def string) string {
	if obj == nil {
		return def
	}
	if intf, ok := obj.(IPlugin_GetPlugID); ok {
		return intf.GetPlugID()
	}
	return def
}

func TrySetPlugID(obj interface{}, val string) bool {
	if intf, ok := obj.(IPlugin_SetID); ok {
		intf.SetPlugID(val)
		return true
	}
	return false
}

type IGetSubscriber interface {
	GetSubscriber() *utils.SubscribeCenter
}

type ISendKVs interface {
	SendKVs(topic string, kv ...interface{}) error
}

type IStatusRequest interface {
	DoStatusRequestRequest(args ...interface{}) (resp interface{})
}

type IDispatchData interface {
	DispatchData(channelid string, sender interface{}, data interface{}, tag interface{}) (err error)
}

type IPickOne interface {
	PickOne(args ...interface{}) interface{}
	GradeOne(val interface{}, grade int, args ...interface{})
	ReleaseOne(val interface{}, args ...interface{})
}

type ISyncMap interface {
	LoadOrStore(id interface{}, store interface{}) (actual interface{}, loaded bool)
	RemoveAndGet(id interface{}) (val interface{}, ok bool)
}

type ISyncMapEx interface {
	LoadOrStore(id interface{}, store interface{}) (actual interface{}, loaded bool)
	RemoveAndGet(id interface{}) (val interface{}, ok bool)
	Range(f func(key, value interface{}) bool)
	Set(id interface{}, val interface{})
	Get(id interface{}, def interface{}) interface{}
	Count() int
}

type IPlgMap interface {
	SetValue(key string, value interface{}) error
	GetValue(key string) (val interface{}, err error)
	RemoveValue(key string) (ok bool, err error)
}

type ActionEventFunc func(sender interface{}, action int, args ...interface{})

type IConsume_Func interface {
	OnConsume(sender interface{}, data interface{}, tag interface{})
}

/*
*

	生产者实现接口, 设置消费分发中心, 进行数据分发
*/
type IProduce_SetConsumeSubscriber interface {
	SetConsumeSubscriber(val *utils.SubscribeCenter)
}

type IActionEventSetter interface {
	SetActionEvent(fn func(sender interface{}, action int, args ...interface{}))
}

func TrySetActionEvent(obj interface{}, fn ActionEventFunc) bool {
	if intf, ok := obj.(IActionEventSetter); ok {
		intf.SetActionEvent(fn)
		return true
	}
	return false
}

const (
	STATUS_ACTION int = 1000
)

type IPlugUtils interface {
	GetReleaseFlag() int
	GetPluginID() string
}

type IPlugGetStatus interface {
	GetPluginStatus(action int) int
}

type IPlugGetVal interface {
	GetPluginVal(action int, args ...interface{}) (ok bool, val interface{})
}

/*
消费者, 设置一个订阅中心, 进行消息订阅
*/
type IConsume_SetProduceSubscriber interface {
	SetProduceSubscriber(val *utils.SubscribeCenter)
}
